<?php
	/**
	*	Excel导入方法和导出方法注意事项
	* 	首先要引入Org文件夹和UploadField文件类
	 */
	
	/*在框架中引入*/

	// namespace Admin\Controller;
	// use User\Api\UserApi;
	// use Think\UploadFile;
	
	class fileController {

		//excel表导入
	    public function daoru()
	    {


	         $upload = new UploadFile();// 实例化上传类
	         $upload->maxSize  = 3145728 ;// 设置附件上传大小
	         $upload->allowExts  = array('xls','xlsx');// 设置附件上传类型
	         if (!file_exists('./Uploads/Last/'.date('Ymd').'/')){ 
	             mkdir('./Uploads/Last/'.date('Ymd').'/'); 
	         }
	         $upload->savePath =  './Uploads/Last/'.date('Ymd').'/';// 设置附件上传目录
	         if(!$info = $upload->upload()) {// 上传错误提示错误信息
	             $this->error($upload->getErrorMsg());
	         }else{// 上传成功 获取上传文件信息
	             $info =  $upload->getUploadFileInfo();
	         }
	        $info[0]['savepath']=substr($info[0]['savepath'], 1); 
	        $filename = $info[0]['savepath'].$info[0]['savename'];
	        $this->showExcel($filename);
	        
	    }

	    //数据管理-因为导入的文件，所以需要一个文件类来打开该文件
	    public function showExcel($filename)
	    {

	        // dump(M('shangpin')->select());exit;
	        header('Content-type:text/html;charset=utf8');
	        import("Org.Util.PHPExcel");
	        import("Org.Util.PHPExcel.Reader.Excel5");
	        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
	        //创建PHPExcel对象，注意，不能少了\
	        $PHPExcel=new \PHPExcel();
	        //如果excel文件后缀名为.xls，导入这个类
	        import("Org.Util.PHPExcel.Reader.Excel2007");
	        $PHPReader=new \PHPExcel_Reader_Excel5();
	        //载入文件
	        $PHPExcel=$PHPReader->load('.'.$filename);
	        //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
	        $currentSheet=$PHPExcel->getSheet(0);
	        //获取总列数
	        $allColumn=$currentSheet->getHighestColumn();
	        //获取总行数
	        $allRow=$currentSheet->getHighestRow();
	        //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
	        for($currentRow=2;$currentRow<=$allRow;$currentRow++){
	            //从哪列开始，A表示第一列
	            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
	                //数据坐标
	                $address=$currentColumn.$currentRow;
	                //读取到的数据，保存到数组$arr中
	                $arr[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
	            }
	        }

	        /////存入临时表
	        $TempModel = D('core_pay_bill_tmp');
	        //$TempModel->startTrans();
	        foreach ($arr as $key => $value) {

	            $data['username'] = $value['A'];
	            $data['age']      = $value['B'];
	            $data['sex']      = $value['C'];
	            $data['aihao']    = $value['D'];
	            $data['zhuanye']  = $value['E'];
	            $data['ctime']    = time();
	            M('studen')->add($data);
	            // dump( M('studen'));exit;


	           // $con['uid'] = $value['A'];
	           // $con['batch'] = $_SESSION['last_batch'];

	           // $mcon['item_c'] = explode('|',$currentSheet->getCell('D1')->getValue())[1];
	           // $result = $TempModel->where($con)->where($mcon)->setInc('last_amount',$value['D']);

	           // $fcon['item_c'] = explode('|',$currentSheet->getCell('E1')->getValue())[1];
	           // $result2 = $TempModel->where($con)->where($fcon)->setInc('last_amount',$value['E']);
	           // dump($value);
	           // if($result&&$result2){
	           //     $TempModel->commit();
	           // }else{
	           //     $TempModel->rollback();
	           //     $this->error('导入失败');
	           }
	       
	       $this->success('导入成功，请仔细检查数据……');
	        
	    }

	    //流水导出
	    public function bill_export()
	    {
	        header('Content-type:text/html;charset=utf8');
	        import("Org.Util.PHPExcel");
	        $data = M('shangpin')->select();
	        $resultPHPExcel = new \PHPExcel(); //一定要加跟杠，不然报错
	        $resultPHPExcel->getActiveSheet()->setCellValue('A1', '姓名');
	        $resultPHPExcel->getActiveSheet()->setCellValue('B1', '班级');
	        $resultPHPExcel->getActiveSheet()->setCellValue('C1', '缴费项');
	        $resultPHPExcel->getActiveSheet()->setCellValue('D1', '总金额');
	        $resultPHPExcel->getActiveSheet()->setCellValue('E1', '明细');
	        $resultPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
	        $resultPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
	        $resultPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
	        $resultPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
	        $resultPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
	        $i = 2;
	        foreach ($data as $item) {
	            $resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['uid']);
	            $resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $item['username']);
	            $resultPHPExcel->getActiveSheet()->setCellValue('C' . $i, $item['shangpinname']);
	            $resultPHPExcel->getActiveSheet()->setCellValue('D' . $i, $item['lujing']);
	            // foreach ($item['detail'] as $key => $value) {
	            //     $a.= $key.':'.$value.'; ';
	            // }
	            $resultPHPExcel->getActiveSheet()->setCellValue('E' . $i,$item['ctime']);
	            //$a = '';
	            $i++;
	        }
	        //设置导出文件名
	        $outputFileName = 'bills.xls';
	        $xlsWriter      = new \PHPExcel_Writer_Excel5($resultPHPExcel);
	        //ob_start(); ob_flush();
	        header("Content-Type: application/force-download");
	        header("Content-Type: application/octet-stream");
	        header("Content-Type: application/download");
	        header('Content-Disposition:inline;filename="' . $outputFileName . '"');
	        header("Content-Transfer-Encoding: binary");
	        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	        header("Pragma: no-cache");
	        $xlsWriter->save("php://output");
	    }
	}
?>